﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using PanasonicWarranty.Business;
using System.Web.UI.HtmlControls;
using Utils;
namespace PanasonicWarranty.UI
{
    public partial class WarrantyStatusManagement : BasePageManageWarranty
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
        }

        void paging_PagingChange(object sender, EventArgs e)
        {
            pagingtop.PagingChange -= new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange -= new EventHandler(paging_PagingChange);
            pagingtop.CurrentPage = (sender as UserControl.ucPaging).CurrentPage;
            pagingbottom.CurrentPage = (sender as UserControl.ucPaging).CurrentPage;
            BindGridData();
            pagingtop.Visible = pagingbottom.Visible = (sender as UserControl.ucPaging).TotalPage > 1;
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PageSize = Convert.ToInt32(tSortable_length.SelectedValue);
                pagingtop.CurrentPage = pagingbottom.CurrentPage = 1;
                pagingtop.PageSize = pagingbottom.PageSize = PageSize;
                pagingtop.PageDisplay = pagingbottom.PageDisplay = 6;
                BindGridData();
            }
        }


        private void BindGridData()
        {
            try
            {
                int currentPage = pagingtop.CurrentPage;
                int pageSize = pagingtop.PageSize;
                int totalRow = 0;
                grvData.DataSource = pa_WarrantyStatus.Paging(currentPage, pageSize, ref totalRow);
                pagingtop.TotalRecord = pagingbottom.TotalRecord = totalRow;
                pagingtop.Visible = pagingbottom.Visible = pagingtop.TotalPage > 1;
                grvData.DataBind();
                ltrInfo.Text = "Tổng cộng: " + pagingtop.TotalPage;
            }
            catch { }
        }
        
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            pagingtop.PagingChange -= new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange -= new EventHandler(paging_PagingChange);
            pagingtop.CurrentPage = pagingbottom.CurrentPage = 1;
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
            BindGridData();
        }

        protected void tSortable_length_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                PageSize = Convert.ToInt32(tSortable_length.SelectedValue);
                pagingtop.PageSize = pagingbottom.PageSize = PageSize;
                BindGridData();
            }
            catch { }
        }

        protected void grvData_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                Authorize(Role.Write);
                int id = Convert.ToInt32(e.CommandArgument);
                hdfID.Value = id.ToString();
                switch (e.CommandName)
                {
                    case "Editing":
                        var obj = DB.pa_WarrantyStatus.FirstOrDefault(p => p.WarrantyStatusID == id);
                        if (obj != null)
                        {
                            txtStatusName.Text = obj.WarrantyStatusName;
                            chkDisable.Checked = obj.Disabled ?? false;
                            RegisterStartUpScript("OpenDialog('dialog1',400);");
                        }
                        break;
                    case "Deleting":
                        obj = DB.pa_WarrantyStatus.FirstOrDefault(p => p.WarrantyStatusID == id);
                        if (obj != null)
                        {
                            DB.pa_WarrantyStatus.DeleteOnSubmit(obj);
                            DB.SubmitChanges();
                            BindGridData();
                        }
                        break;
                }
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Authorize(Role.Write);
                if (ValidateInfo())
                {
                    pa_WarrantyStatus obj;
                    if (hdfID.Value == "")//Add new
                    {
                        obj = new pa_WarrantyStatus();
                        obj.WarrantyStatusName = txtStatusName.Text.Trim();
                    }
                    else //Edit
                    {
                        obj = DB.pa_WarrantyStatus.FirstOrDefault(p => p.WarrantyStatusID == Convert.ToInt32(hdfID.Value));
                    }
                    obj.WarrantyStatusName = txtStatusName.Text;
                    obj.Disabled = chkDisable.Checked;
                    //Save
                    if (hdfID.Value == "")
                        DB.pa_WarrantyStatus.InsertOnSubmit(obj);
                    DB.SubmitChanges();
                    BindGridData();
                }
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }

        protected void lbtnAddNew_Click(object sender, EventArgs e)
        {
            try
            {
                Authorize(Role.Write);
                ControlClass.ResetControlValues(pnlDialogBlock);
                txtStatusName.Enabled = true;
                RegisterStartUpScript("OpenDialog('dialog1',400);");
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }

        protected override bool ValidateInfo()
        {
            string errMsg = string.Empty;
            if (string.IsNullOrWhiteSpace(txtStatusName.Text))
                errMsg = "Bắt buộc nhập tên trạng thái";
            if (errMsg != string.Empty)
            {
                lbError.Text = errMsg;
                RegisterStartUpScript("OpenDialog('dialog1',400);");
                return false;
            }
            else
            {
                lbError.Text = string.Empty;
                return true;
            }
        }
    }
}